package logic

import (
	"context"
	"edu-pro/edurpc/edurpc"
	"edu-pro/edurpc/internal/svc"
	"edu-pro/models/mysql"
	"errors"

	"github.com/zeromicro/go-zero/core/logx"
)

type CourseAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCourseAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CourseAddLogic {
	return &CourseAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CourseAddLogic) CourseAdd(in *edurpc.CourseAddRequest) (*edurpc.CourseAddResponse, error) {
	//讲师课程发布
	var course mysql.Course
	mysql.DB.Debug().Where("title = ?", in.Title).Find(&course)
	if course.ID != 0 {
		return nil, errors.New("该课程已存在")
	}
	if in.Title == "" || in.Price == 0 || in.Intro == "" || in.Category == "" {
		return nil, errors.New("必填项不能为空")
	}
	course = mysql.Course{
		Title:         in.Title,
		UserId:        int(in.UserId),
		Category:      in.Category,
		Price:         float64(in.Price),
		OriginalPrice: float64(in.OriginalPrice),
		CoverUrl:      in.CoverUrl,
		Intro:         in.Intro,
	}
	mysql.DB.Debug().Create(&course)

	return &edurpc.CourseAddResponse{
		CourseId: int64(course.ID),
	}, nil
}
